package com.example.rabbitmq4.controller;

import com.example.rabbitmq4.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/ack")
    public String confirm() {
        rabbitTemplate.convertAndSend("", Constants.ACK_QUEUE,"rabbit ack");
        return "消息发送成功";
    }

    @RequestMapping("/permanent")
    public String permanent() {
        String msg = "rabbitmq permanent";
        Message message = new Message(msg.getBytes(),new MessageProperties());
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.PERMANENT_EXCHANGE,"permanent",message);
        return "消息发送成功";
    }
}
